package com.dhcc.bpm.modules.scf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.utils.StringUtils;
import com.dhcc.bpm.modules.scf.dto.ScfPaymentInfoDTO;
import com.dhcc.bpm.modules.scf.entity.ScfPaymentInfo;
import com.dhcc.bpm.modules.scf.service.IScfPaymentInfoService;
import com.dhcc.bpm.modules.scf.vo.ScfPaymentInfoVO.ScfGetPaymentDetailsVO;
import com.dhcc.bpm.modules.scf.vo.ScfPaymentInfoVO.ScfGetPaymentListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 支付信息表Controller
 */
@RestController
@AllArgsConstructor
public class ScfPaymentInfoController {

    private IScfPaymentInfoService scfPaymentInfoService;

    /**
     * 分页查询支付信息列表
     *
     * @param scfPaymentInfo
     * @param query
     * @return   采购 买 我们对应的是付款回单
     *
     * 供销  对应的是收款回单
     */
    @GetMapping("/scfGetPaymentList")
    // @Cacheable(value = "scfPaymentListCache", key = "'scfPaymentListCacheKey-' + #scfPaymentInfo.hashCode() + '-' + #query.hashCode()", unless = "#result == null")
    public R<IPage<ScfGetPaymentListVO>> getAllPaymentInfo(ScfPaymentInfoDTO scfPaymentInfo, Query query) {
        if (StringUtils.isEmpty( scfPaymentInfo.getOrgCode() ) ) {
            return  R.fail(400,"公司编码不能为空") ;
        }
        scfPaymentInfo.setOrgCodes(  scfPaymentInfo.getOrgCode().split(",") );


        if(scfPaymentInfo.getOrgCode().contains( "50000000")){
            scfPaymentInfo.setOrgCode("50000000");
            scfPaymentInfo.setOrgCodes("50000000".split(",") );
        }

        return R.data(scfPaymentInfoService.selectScfPaymentInfoPage(Condition.getPage(query), scfPaymentInfo));
    }

    /**
     * 获取支付信息详情
     *
     * @param scfPaymentInfo
     * @return
     */
    @GetMapping("/scfGetPaymentDetails")
    // @Cacheable(value = "scfPaymentDetailsCache", key = "'scfPaymentDetailsCacheKey-' + #scfPaymentInfo.rowId")
    public R<ScfGetPaymentDetailsVO> details(ScfPaymentInfoDTO scfPaymentInfo) throws IOException {
        return R.data(scfPaymentInfoService.getDetail(scfPaymentInfo));
    }

    /**
     * 添加支付信息详情
     *
     * @param scfPaymentInfo
     */
    @PostMapping("/scfAddPaymentDetails")
    // @CacheEvict(value = "scfPaymentListCache", allEntries = true)
    public R<Boolean> addPaymentInfo(@RequestBody ScfPaymentInfoDTO scfPaymentInfo) throws Exception {
        return R.status(scfPaymentInfoService.savePaymentInfo(scfPaymentInfo));
    }

    /**
     * 更新支付信息详情
     *
     * @param scfPaymentInfo
     */
    @PostMapping("/scfModPaymentDetails")
    // @CacheEvict(value = "scfPaymentDetailsCache", key = "'scfPaymentDetailsCacheKey-' + #scfPaymentInfo.rowId")
    public R<Boolean> updatePaymentBatchInfo(@RequestBody ScfPaymentInfoDTO scfPaymentInfo) {
        return R.status(scfPaymentInfoService.updatePaymentInfo(scfPaymentInfo));
    }

    /**
     * 批量导入支付信息
     *
     * @param operatorId 上传用户的id
     * @param file       文件
     * @return
     * @throws IOException
     */
    @PostMapping("/scfPaymentBatchFile")
    // @CacheEvict(value = "scfFileInfoListCache", allEntries = true)
    public R<Boolean> uploadFile(@RequestParam("operatorId") String operatorId, MultipartFile file) throws Exception {
        // 获取到批量导入的数据集合
        List<ScfPaymentInfo> scfPaymentInfoS = scfPaymentInfoService.batchSaveScfPaymentInfo(operatorId, file);
        int bachSavePaymentInfo = scfPaymentInfoService.bachSavePaymentInfo(scfPaymentInfoS, operatorId);
        int total = scfPaymentInfoS.size();
        int totalSussess = bachSavePaymentInfo;
        int totalFail = total - totalSussess;
        String msg = "解析到" + total + "条数据，" + "导入成功" + totalSussess + "条，导入失败" + totalFail + "条！";
        return R.success(msg);
    }

    @PostMapping("/scfPaymentInfo/remove")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {

        String[] split = ids.split(",");

        return R.status(scfPaymentInfoService.deleteByIds( split ));
    }
}
